<script setup lang="ts">
import { postSigninAPI, sendPhoneCodeAPI, postLoginWxMinSimpleAPI } from '@/services/login'
import { useMemberStore } from '@/stores'
import type { LoginResult, SigninResult, sendPhoneCodeResult } from '@/types/member'
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'

const signinSuccess = (profile: SigninResult) => {
  // 保存会员信息
  // const memberStore = useMemberStore()
  // memberStore.setProfile(profile)
  // 成功提示
  uni.showToast({ icon: 'success', title: '注册成功，联系管理员激活账号' })
  // setTimeout(() => {
  // 页面跳转
  // uni.switchTab({ url: '/pages/my/my' })
  // uni.navigateBack()
  // }, 500)
}
const disabledGotoSinginButton = ref<boolean>(true)
// 传统表单登录，测试账号：13123456789 密码：123456，测试账号仅开发学习使用。
const form = ref({
  phoneNumber: '',
  real_name: '',
  phoneCode: '',
  inviter: '',
})

// 表单提交(传统的登录)
const onSubmit = async () => {
  const res = await postSigninAPI(form.value)
  console.log('提交后返回结果：', res)
  signinSuccess(res.results)
}
// 去登录按钮事件
const onLogin = () => {
  uni.navigateTo({
    url: '/pages/login/login',
  })
}
// 只要有结果说明已经成功
const sendPhoneCodeSuccess = (obj: object) => {
  // 成功提示
  uni.showToast({ icon: 'success', title: '发送成功，请查看短信息' })
  // console.log('结果:', obj)
}
// 发送短信验证码
const onSendCode = async () => {
  if (form.value.phoneNumber.length == 0 || form.value.real_name.length == 0) {
    uni.showToast({ icon: 'error', title: '缺姓名和手机号码' })
    return 0
  }
  const data = { phoneNumber: form.value.phoneNumber }
  // console.log('发送手机号码', data)
  const res = await sendPhoneCodeAPI(data)
  if (res.results) {
    sendPhoneCodeSuccess(res.results)
    disabledGotoSinginButton.value = false
  } else {
    uni.showToast({ icon: 'error', title: '发送失败' })
  }

}
</script>

<template>
  <view class="viewport">
    <view class="logo">
      <image src="../../static/images/5.png"></image>
    </view>
    <view class="login">
      <!-- 姓名、手机号码和验证码注册 -->
      <input v-model="form.real_name" class="input" type="text" placeholder="请输入真实姓名" />
      <input v-model="form.phoneNumber" class="input" type="text" placeholder="请输入手机号码" />
      <view class="send_code">
        <input v-model="form.phoneCode" class="input_code" type="text" placeholder="请输入短信验证码" />
        <button @tap="onSendCode" class="button_send">发送</button>
      </view>
      <input v-model="form.inviter" class="input" type="text" placeholder="邀请者手机号码" />
      <view class="buttons">
        <button @tap="onSubmit" type="primary" size="mini" :disabled="disabledGotoSinginButton">注册</button>
        <button @tap="onLogin" type="warn" size="mini">去登录</button>
      </view>
      <view class="others">
        <uni-icons type="notification-filled" size="13"></uni-icons>
        <text class="text_blue">默认账号密码为123456，可修改；</text>
        <text class="text_green">默认账号类型为学生，联系管理员改为教师类型</text>
      </view>
    </view>
  </view>
</template>

<style lang="scss" scoped>
page {
  height: 100%;
}

.viewport {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: 20rpx 40rpx;
}

.logo {
  flex: 1;
  text-align: center;

  image {
    width: 220rpx;
    height: 220rpx;
    margin-top: 15vh;
  }
}

.login {
  display: flex;
  flex-direction: column;
  height: 60vh;
  padding: 40rpx 20rpx 20rpx;

  .input {
    width: 100%;
    height: 80rpx;
    font-size: 28rpx;
    border-radius: 72rpx;
    border: 1px solid #ddd;
    padding-left: 30rpx;
    margin-bottom: 20rpx;

    left {
      width: 60%;
    }

    right {
      width: 40%;
    }
  }

  .send_code {
    display: flex;
    flex-flow: row nowrap;
    align-items: center;
    justify-content: left;
  }

  .input_code {
    width: 70%;
    height: 80rpx;
    font-size: 28rpx;
    border-radius: 72rpx;
    border: 1px solid #ddd;
    padding-left: 30rpx;
    margin-bottom: 20rpx;
  }

  .button_send {
    width: 30%;
    height: 80rpx;
    font-size: 28rpx;
    border-radius: 72rpx;
    background-color: #28bb9c;
  }

  .button_login {
    width: 30%;
    height: 80rpx;
    font-size: 28rpx;
    border-radius: 72rpx;
    background-color: #ecdc29c8;

  }

  .button {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 80rpx;
    font-size: 28rpx;
    border-radius: 72rpx;
    color: #fff;

    .icon {
      font-size: 40rpx;
      margin-right: 6rpx;
    }
  }

  .phone {
    background-color: #28bb9c;
  }

  .wechat {
    background-color: #06c05f;
  }

  .extra {
    flex: 1;
    padding: 70rpx 70rpx 0;

    .caption {
      width: 440rpx;
      line-height: 1;
      border-top: 1rpx solid #ddd;
      font-size: 26rpx;
      color: #999;
      position: relative;

      text {
        transform: translate(-40%);
        background-color: #fff;
        position: absolute;
        top: -12rpx;
        left: 50%;
      }
    }

    .options {
      display: flex;
      justify-content: center;
      align-items: center;
      margin-top: 70rpx;

      button {
        padding: 0;
        background-color: transparent;

        &::after {
          border: none;
        }
      }
    }

    .icon {
      font-size: 24rpx;
      color: #444;
      display: flex;
      flex-direction: column;
      align-items: center;

      &::before {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 80rpx;
        height: 80rpx;
        margin-bottom: 6rpx;
        font-size: 40rpx;
        border: 1rpx solid #444;
        border-radius: 50%;
      }
    }

    .icon-weixin::before {
      border-color: #06c05f;
      color: #06c05f;
    }
  }
}

@keyframes animate__shakeY {
  0% {
    transform: translate(0, 0);
  }

  50% {
    transform: translate(0, -5rpx);
  }

  100% {
    transform: translate(0, 0);
  }
}

.animate__shakeY {
  animation: animate__shakeY 0.2s ease-in-out 3;
}

.button-privacy-wrap {
  position: relative;

  .button-opacity {
    opacity: 0;
    position: absolute;
    z-index: 1;
  }
}

.tips {
  position: absolute;
  bottom: 80rpx;
  left: 20rpx;
  right: 20rpx;
  font-size: 22rpx;
  color: #999;
  text-align: center;

  .radio {
    transform: scale(0.6);
    margin-right: -4rpx;
    margin-top: -4rpx;
    vertical-align: middle;
  }

  .link {
    display: inline;
    color: #28bb9c;
  }
}

.buttons {
  display: flex;
  flex-flow: row nowrap;
  justify-content: space-around;
  margin: 3rpx;
}

.others {
  display: flew;
  flex-flow: row wrap;
  margin: 2rpx;
}

.text_blue {
  color: blue;
  font-size: 32rpx;
}

.text_green {
  color: #28bb9c;
  font-size: 32rpx;
}
</style>
